package com.hut.bms.service;

import com.hut.bms.enums.BookStatus;
import com.hut.bms.mapper.BookInfoMapper;
import com.hut.bms.model.BookInfo;
import com.hut.bms.model.PageRequest;
import com.hut.bms.model.PageResult;
import com.hut.bms.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelectBookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    public PageResult<BookInfo> selectBookByPage(PageRequest pageRequest) {
        //获取图书总数
        Integer count = bookInfoMapper.count();
        //获取当前页面的图书
        List<BookInfo> bookInfoList =  bookInfoMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        //处理借阅状态
        for(BookInfo bookInfo : bookInfoList) {
            bookInfo.setStateCN(BookStatus.getDescByCode1(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfoList,count,pageRequest);
    }
    //查看以id为参数的图书信息
    public BookInfo getBookById(Integer bookId) {
        BookInfo bookInfo = bookInfoMapper.getBookInfoById(bookId);
        //处理借阅状态
        bookInfo.setStateCN(BookStatus.getDescByCode1(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }
}
